<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="软件名字" prop="softName">
      <el-input v-model="dataForm.softName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="所属栏目" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="软件logo" prop="softLogo">
      <el-input v-model="dataForm.softLogo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="版本信息" prop="edition">
      <el-input v-model="dataForm.edition" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="软件语言" prop="language">
      <el-input v-model="dataForm.language" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="支持平台" prop="platform">
      <el-input v-model="dataForm.platform" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="软件大小" prop="softSize">
      <el-input v-model="dataForm.softSize" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="下载地址" prop="softAddr">
      <el-input v-model="dataForm.softAddr" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="发布者ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="审核状态" prop="verify">
      <el-input v-model="dataForm.verify" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="下载次数" prop="downloads">
      <el-input v-model="dataForm.downloads" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="安装步骤" prop="installProleft">
      <el-input v-model="dataForm.installProleft" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="界面展示" prop="showPic">
      <el-input v-model="dataForm.showPic" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="安装步骤" prop="installProright">
      <el-input v-model="dataForm.installProright" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="简短介绍" prop="briefIntro">
      <el-input v-model="dataForm.briefIntro" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          softId: 0,
          softName: '',
          parentId: '',
          softLogo: '',
          edition: '',
          language: '',
          platform: '',
          softSize: '',
          softAddr: '',
          userId: '',
          createTime: '',
          updateTime: '',
          verify: '',
          downloads: '',
          installProleft: '',
          showPic: '',
          installProright: '',
          briefIntro: ''
        },
        dataRule: {
          softName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          softLogo: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          edition: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          language: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          platform: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          softSize: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          softAddr: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          verify: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          downloads: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          installProleft: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          showPic: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          installProright: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          briefIntro: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.softId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.softId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/software/info/${this.dataForm.softId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.softName = data.software.softName
                this.dataForm.parentId = data.software.parentId
                this.dataForm.softLogo = data.software.softLogo
                this.dataForm.edition = data.software.edition
                this.dataForm.language = data.software.language
                this.dataForm.platform = data.software.platform
                this.dataForm.softSize = data.software.softSize
                this.dataForm.softAddr = data.software.softAddr
                this.dataForm.userId = data.software.userId
                this.dataForm.createTime = data.software.createTime
                this.dataForm.updateTime = data.software.updateTime
                this.dataForm.verify = data.software.verify
                this.dataForm.downloads = data.software.downloads
                this.dataForm.installProleft = data.software.installProleft
                this.dataForm.showPic = data.software.showPic
                this.dataForm.installProright = data.software.installProright
                this.dataForm.briefIntro = data.software.briefIntro
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/software/${!this.dataForm.softId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'softId': this.dataForm.softId || undefined,
                'softName': this.dataForm.softName,
                'parentId': this.dataForm.parentId,
                'softLogo': this.dataForm.softLogo,
                'edition': this.dataForm.edition,
                'language': this.dataForm.language,
                'platform': this.dataForm.platform,
                'softSize': this.dataForm.softSize,
                'softAddr': this.dataForm.softAddr,
                'userId': this.dataForm.userId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'verify': this.dataForm.verify,
                'downloads': this.dataForm.downloads,
                'installProleft': this.dataForm.installProleft,
                'showPic': this.dataForm.showPic,
                'installProright': this.dataForm.installProright,
                'briefIntro': this.dataForm.briefIntro
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
